package weaver.aiyh_pcn.async_organization.config;

import aiyh.utils.httpUtil.ResponeVo;
import aiyh.utils.httpUtil.util.HttpUtils;
import aiyh.utils.zwl.common.ToolUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.ibm.icu.text.SimpleDateFormat;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import weaver.aiyh_pcn.async_organization.result.ResultBean;

import java.io.IOException;
import java.util.*;

/**
 * @author EBU7-dev1-ayh
 * @create 2021/7/20 0020 16:40
 * 同步第三方系统人员组织结构配置类
 */


public class SyncOrganizationConfig<E> extends ToolUtil {

	/**
	 * api验证参数
	 */
	private final String API_KEY = this.getSystemParamValue("CMS_API_KEY");
	/**
	 * 测试地址URL
	 */
	private String CMS_TEST_URL = this.getSystemParamValue("CMS_TEST_URL");
	/**
	 * 生产环境地址URL
	 */
	private String CMS_PROUD_URL = this.getSystemParamValue("CMS_PROUD_URL");
	/**
	 * 员工
	 */
	private String EmployeeApi = this.getSystemParamValue("EmployeeApi");
	/**
	 * 部门
	 */
	private String DepartmentApi = this.getSystemParamValue("DepartmentApi");
	/**
	 * 职位
	 */
	private String PositionApi = this.getSystemParamValue("PositionApi");
	private String CMS_API_URL_MARK = this.getSystemParamValue("CMS_API_URL_MARK");
	private String REQUEST_API_RUL = "";
	/**
	 * 每页数据大小
	 */
	private final String PAGE_SIZE = this.getSystemParamValue("CMS_API_GET_DATA_SIZE");
	private String CMS_API_QUERY_DATA = "";
	private HttpUtils httpUtils = new HttpUtils();

	public SyncOrganizationConfig() {
		if (0 == Integer.parseInt(CMS_API_URL_MARK)) {
			this.REQUEST_API_RUL = this.CMS_TEST_URL;
			this.CMS_API_QUERY_DATA = "?date=" + this.getSystemParamValue("CMS_API_QUERY_DATA");
		} else {
			this.REQUEST_API_RUL = this.CMS_PROUD_URL;
			this.CMS_API_QUERY_DATA = this.getDate();
		}
	}

	private String getDate() {
		Date date = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
		return "?date=" + formatter.format(date);
	}

	//	封装整理接口数据
	public List<E> getDataList(String url) {
		int pageIndex = 1;
		ArrayList<E> employees = new ArrayList<>();
		while (true) {
			ResultBean<E> employeeResultBean = getDataResult(pageIndex, url);
//			this.writeErrorLog("人员组织架构的响应互数据：" + employeeResultBean);
			assert employeeResultBean != null;
			employees.addAll(employeeResultBean.getItems());
			pageIndex++;
//			|| employeeResultBean.getItems().size() < employeeResultBean.getPageSize()
			if (employeeResultBean.getPage() >= employeeResultBean.getTotalPage() ) {
				break;
			}
		}
		return employees;
	}


	//	请求api接口数据
	private ResultBean<E> getDataResult(int pageIndex, String url) {
		this.writeErrorLog("人员组织架构请求地址: " + url + " 请求数据：PAGE_SIZE：" + PAGE_SIZE + " pageIndex：" + pageIndex + " apikey：" + API_KEY);
//		DefaultHttpClient httpClient = HttpManager.getHttpClient();

//		HttpPost httpPost = new HttpPost(url);
		Map<String, Object> params = new HashMap(2);
		Map<String, String> heards = new HashMap(2);
		heards.put("apikey", API_KEY);
		heards.put("Content-Type", "application/json");
		params.put("pageSize", PAGE_SIZE);
		params.put("pageIndex", Integer.toString(pageIndex));


//		List<NameValuePair> nvps = new ArrayList<>();
//		nvps.add(new BasicNameValuePair("pageSize","10"));
//		nvps.add(new BasicNameValuePair("pageIndex","1"));
//      httpPost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
/*		httpPost.setHeader("apikey", API_KEY);
		httpPost.setHeader("Content-Type", "application/json");
		httpPost.setEntity(new StringEntity(JSON.toJSONString(params), HTTP.UTF_8));*/
		CloseableHttpResponse execute = null;
		try {
//			execute = httpClient.execute(httpPost);
			ResponeVo responeVo = httpUtils.apiPost(url, params, heards);
			if (responeVo.getCode() == 200) {
				return JSON.parseObject(responeVo.getEntityString(), new TypeReference<ResultBean<E>>() {
				});
			}
/*			if (execute.getStatusLine().getStatusCode() == 200) {
				HttpEntity entity = execute.getEntity();
				String response = EntityUtils.toString(entity, "utf-8");

				return JSON.parseObject(response, new TypeReference<ResultBean<E>>() {
				});
			}*/
//			HttpEntity entity = execute.getEntity();
//			String response = EntityUtils.toString(entity, "utf-8");
			this.writeErrorLog("人员组织架构的相应数据:" + responeVo.getEntityString());
//			System.out.println(url);
//			System.out.println(response);
//			System.out.println(JSON.parseObject(response, new TypeReference<ResultBean<E>>() {}));
			return null;
		} catch (IOException e) {
			try {
				if (execute != null) {
					execute.close();
				}
			} catch (IOException ioException) {
				ioException.printStackTrace();
				return null;
			}
			e.printStackTrace();
			this.writeErrorLog("转换错误！错误原因：" + e);
			return null;
		}
	}

	public String getEMPLOYEE_API_RUL() {
		return this.REQUEST_API_RUL + this.EmployeeApi + this.CMS_API_QUERY_DATA;
	}

	public String getDEPARTMENT_API_URL() {
		return this.REQUEST_API_RUL + this.DepartmentApi + this.CMS_API_QUERY_DATA;
	}

	public String getPOSITION_API_URL() {
		return this.REQUEST_API_RUL + this.PositionApi + this.CMS_API_QUERY_DATA;
	}
}
